package com.feihong.txgw.shopcar.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.TextView;

import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.shopcar.bean.TerminalEntiyt;
import com.feihong.txgw.shopcar.viewholder.ShopCarRecommendHeaderViewHolder;
import com.feihong.txgw.ui.activity.product.ProductInfoActivity;
import com.feihong.txgw.ui.activity.product.ProductListActivity;
import com.feihong.txgw.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

import static com.feihong.txgw.ui.fragment.home.DiscoverFragment.INTENT_PRODUCTID;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.shopcar.adapter
 * @class describe: 购物车推荐商品adapter
 * @author wangziheng
 * @time 2017/7/3 19:22
 * @change
 * @chang time
 * @class describe
 */
public class ShopCarRecommendAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private Context mContext;
    private List<TerminalEntiyt> mGoodsEntitys;

    /**
     *  头部
     */
    private static final int TYPE_HEADER = 0;
    /**
     * 列表
     */
    private static final int TYPE_LIST = 1;
    private static final int TYPE_FOOTER = 2;

    public ShopCarRecommendAdapter(Context context) {
        mContext = context;
        mGoodsEntitys = new ArrayList<>();
    }

    public void setGoodArray(List<TerminalEntiyt> goods) {
        mGoodsEntitys.clear();
        mGoodsEntitys.addAll(goods);
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_LIST)
//            return new ShopCarRecommendListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_recommend_item,parent,false));
        {
            return new ShopCarRecommendListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_recommend_item, parent, false));
        }
        return new ShopCarRecommendHeaderViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_null_status_recommend_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_LIST) {
            ShopCarRecommendListViewHolder mHolder = (ShopCarRecommendListViewHolder) holder;
            mHolder.mQueryMoreData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, ProductListActivity.class);
                    mContext.startActivity(intent);
                }
            });
            mHolder.initData(mGoodsEntitys);
        } else if (getItemViewType(position) == TYPE_HEADER) {
            ShopCarRecommendHeaderViewHolder mHolder = (ShopCarRecommendHeaderViewHolder) holder;
            mHolder.mGoToSelectGoods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, ProductListActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (0 == position) {
            return TYPE_HEADER;
        }
        return TYPE_LIST;
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    /**
     * 推荐商品列表viewholder
     */
    protected class ShopCarRecommendListViewHolder extends BaseViewHolder {

        public NoScrollGridView mGridView;
        public TextView mQueryMoreData;

        private ShopCarRecommendSecondAdapter mSecondAdapter;

        public ShopCarRecommendListViewHolder(View itemView) {
            super(itemView);
            mSecondAdapter = new ShopCarRecommendSecondAdapter(mContext);
            mGridView = (NoScrollGridView) itemView.findViewById(R.id.shopcar_recommend_holder_gridview);
            mQueryMoreData = (TextView) itemView.findViewById(R.id.more_data);
        }

        public void initData(List<TerminalEntiyt> mGoodsEntitys) {
            if(mGoodsEntitys == null || mGoodsEntitys.size() < 1){
                mQueryMoreData.setVisibility(View.GONE);
                return;
            }
            mQueryMoreData.setVisibility(View.VISIBLE);
            mGridView.setAdapter(mSecondAdapter);
            mSecondAdapter.setDatas(mGoodsEntitys);

            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Adapter adapter = adapterView.getAdapter();
                    TerminalEntiyt terminalEntiyt = (TerminalEntiyt) adapter.getItem(i);
                    mContext.startActivity(new Intent(mContext, ProductInfoActivity.class).putExtra(INTENT_PRODUCTID,terminalEntiyt.getSku_id()).putExtra("shopCarRes", 110));
                }
            });
        }
    }
}
